package com.itheima.web.controller.store;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.itheima.core.BaseServlet;
import com.itheima.domain.store.Catalog;
import com.itheima.domain.store.Company;
import com.itheima.domain.store.Question;
import com.itheima.service.store.CatalogService;
import com.itheima.service.store.CompanyService;
import com.itheima.service.store.QuestionService;
import com.itheima.service.store.impl.CatalogServiceImpl;
import com.itheima.service.store.impl.CompanyServiceImpl;
import com.itheima.service.store.impl.QuestionServiceImpl;
import com.itheima.utils.BeanUtil;
import com.itheima.utils.UUIDUtil;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.lang.reflect.Field;
import java.util.List;

@WebServlet("/store/question/*")
public class QuestionServlet extends BaseServlet {
    private QuestionService questionService = new QuestionServiceImpl();
    private CompanyService companyService = new CompanyServiceImpl();//企业
    private CatalogService catalogService = new CatalogServiceImpl();//目录，阶段，类别


    /**
     * 显示列表信息
     */
    public String list(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");

        if(StringUtils.isBlank(pageNum)){
            pageNum = "1";
        }
        if(StringUtils.isBlank(pageSize)){
            pageSize = "10";
        }

        PageInfo page = questionService.queryPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        request.setAttribute("page",page);
        return "/WEB-INF/pages/store/question/list.jsp";
    }

    /**
     * 根据ID回显数据
     */
    public String toEdit(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String id = request.getParameter("id");

        /*查询当前问题信息*/
        Question question = questionService.findById(id);
        request.setAttribute("question",question);

        List<Company> companies = companyService.findAll();
        List<Catalog> catalogs = catalogService.findAll();
        request.setAttribute("catalogList",catalogs);
        request.setAttribute("companyList",companies);

        return "/WEB-INF/pages/store/question/update.jsp";
    }

    /**
     * 更新数据，更新完毕后跳转到列表页面
     */
    public String edit(HttpServletRequest request, HttpServletResponse response)throws Exception{
        Question questionInForm = BeanUtil.fillBean(request, Question.class, "yyyy-MM-dd");
        questionService.update(questionInForm);
        return "/store/question/list";
    }

    /**
     * 跳转到新建add.jsp页面
     */
    public String toAdd(HttpServletRequest request, HttpServletResponse response)throws Exception{
        List<Company> companies = companyService.findAll();
        List<Catalog> catalogs = catalogService.findAll();
        request.setAttribute("catalogList",catalogs);
        request.setAttribute("companyList",companies);

        return "/WEB-INF/pages/store/question/add.jsp";
    }

    /**
     * 保存，保存完毕后跳转到列表页面
     */
    public String save(HttpServletRequest request, HttpServletResponse response)throws Exception{
        List<FileItem> fileItems = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
        for (FileItem fileItem : fileItems) {
            if(fileItem.isFormField()==false){
                File file = new File(super.getServletContext().getRealPath("upload"), fileItem.getName());

                System.out.println("当前上传文件的名字:"+fileItem.getName());
                System.out.println("当前上传文件的位置:"+file.getAbsolutePath());
                fileItem.write(file);
            }
        }
        return "/store/question/list";
    }

    /**
     * 删除，删除完毕跳转到列表页面
     */
    public String delete(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String[] ids = request.getParameterValues("id");
        questionService.deleteById(ids);
        return "/store/question/list";
    }

}
